<?php
/**
 * Plugin Name: 幻影微信公众号插件
 * Plugin URI: https://www.ailingxi.cn
 * Description: 微信公众号接收消息并搜索WordPress网站内容，反馈搜索结果给用户
 * Version: 1.0.0
 * Author: HuanYing
 * Author URI: https://www.52hyjs.com
 * License: GPL2
 */

// 安全检查
if (!defined('ABSPATH')) {
    exit;
}

// 定义插件常量
define('HYWL_WECHAT_VERSION', '1.0.0');
define('HYWL_WECHAT_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('HYWL_WECHAT_PLUGIN_URL', plugin_dir_url(__FILE__));

// 包含必要文件
require_once HYWL_WECHAT_PLUGIN_DIR . 'includes/class-hywl-wechat-logger.php';
require_once HYWL_WECHAT_PLUGIN_DIR . 'includes/class-hywl-wechat.php';
require_once HYWL_WECHAT_PLUGIN_DIR . 'admin/class-hywl-wechat-admin.php';

// 激活插件时执行
register_activation_hook(__FILE__, 'hywl_wechat_activate');
function hywl_wechat_activate() {
    // 添加重写规则
    add_rewrite_rule('^wechat-api/?', 'index.php?wechat-api=1', 'top');
    flush_rewrite_rules();
    
    // 初始化选项
    $default_options = array(
        'token' => wp_generate_password(16, false),
        'appid' => '',
        'appsecret' => '',
        'welcome_msg' => '欢迎关注！请发送关键词搜索相关内容。',
        'not_found_msg' => '抱歉，未找到相关内容，请尝试其他关键词。',
        'search_limit' => 5,
        'log_enabled' => 1,
        'max_log_size' => 10
    );
    
    add_option('hywl_wechat_options', $default_options);
    
    // 记录激活日志
    error_log('微信公众号插件已激活 - ' . date('Y-m-d H:i:s'), 3, HYWL_WECHAT_PLUGIN_DIR . 'logs/activation.log');
}

// 停用插件时执行
register_deactivation_hook(__FILE__, 'hywl_wechat_deactivate');
function hywl_wechat_deactivate() {
    flush_rewrite_rules();
}

// 添加调试辅助函数
function hywl_wechat_debug_log($message) {
    $log_file = HYWL_WECHAT_PLUGIN_DIR . 'logs/debug.log';
    $log_message = '[' . date('Y-m-d H:i:s') . '] ' . $message . "\n";
    file_put_contents($log_file, $log_message, FILE_APPEND);
}

// 初始化插件
function hywl_wechat_init() {
    // 记录插件初始化
    hywl_wechat_debug_log('插件初始化');
    
    // 注册查询变量
    add_filter('query_vars', function($vars) {
        $vars[] = 'wechat-api';
        return $vars;
    });
    
    // 添加自定义接口处理路径
    add_action('init', function() {
        add_rewrite_rule('^wechat-api/?$', 'index.php?wechat-api=1', 'top');
        // 检查是否需要刷新规则
        $rules_version = get_option('hywl_wechat_rules_version', '');
        if ($rules_version != HYWL_WECHAT_VERSION) {
            flush_rewrite_rules();
            update_option('hywl_wechat_rules_version', HYWL_WECHAT_VERSION);
            hywl_wechat_debug_log('刷新重写规则');
        }
    });
    
    // 处理微信API请求
    add_action('template_redirect', function() {
        hywl_wechat_debug_log('Template redirect: ' . $_SERVER['REQUEST_URI']);
        
        if (get_query_var('wechat-api') || strpos($_SERVER['REQUEST_URI'], 'wechat-api') !== false) {
            hywl_wechat_debug_log('接收到微信API请求: ' . $_SERVER['REQUEST_METHOD']);
            $hywl_wechat = new HYWL_Wechat();
            $hywl_wechat->handle_request();
            exit;
        }
    });
    
    // 初始化管理页面
    if (is_admin()) {
        $admin = new HYWL_Wechat_Admin();
        $admin->init();
    }
}
add_action('init', 'hywl_wechat_init'); 